export function request(path, data, header) {
  const { token } = uni.getStorageSync('token') || {};
  const pathList = path.split('/');
  if (pathList[0] === '') pathList.shift();
  const name = pathList.shift();
  const action = pathList.join('/');
  return new Promise((resolve, reject) => {
    uniCloud
      .callFunction({
        name: name,
        data: {
          action: action,
          data: data,
          header: header,
          uniIdToken: token,
        },
      })
      .then((res) => {
        console.log(res.result);
        const { code, message } = res.result;
        const errorCode = [50202];
        if (code === 'TOKEN_INVALID') {
          reject(res.result);
          uni.navigateTo({ url: '/pages/login/index' });
          uni.removeStorage({
            key: 'token',
            success() {
              uni.showToast({ title: message, icon: 'none' });
            },
          });
          return false;
        }
        if (errorCode.includes(code)) {
          reject(res.result);
          return uni.showToast({ title: message, icon: 'none' });
        }
        resolve(res.result);
      })
      .catch((err) => {
        console.log(err);
        reject(err);
      });
  });
}
